package com.cqs.leetcode.array;

public class LongestMountainArray845 {


    private int result = 0;

    public int longestMountain(int[] arr) {
        return longestMountain(0, arr);
    }

    /**
     * 双指针+递归处理
     *
     * @param start
     * @param arr
     * @return
     */
    private int longestMountain(int start, int[] arr) {
        //线剪切
        if (arr.length - start < 3) return 0;
        //将不符合左边单调递减的部分裁剪掉
        while (start < arr.length - 1 && arr[start] >= arr[start + 1]) {
            ++start;
        }
        //单调递减部分
        int left = start;
        while (start < arr.length - 1 && arr[start] < arr[start + 1]) {
            ++start;
        }
        //求右边严格单调递增的部分
        int right = start;

        //若[3,2,1]这个的单调数组，进入不了循环，start=arr.length-1;
        while (right < arr.length - 1 && arr[right] > arr[right + 1]) {
            ++right;
        }

        //说明没有出现单调递减 本轮的计算子数组有效长度为0
        if (right != start) {
            result = Math.max(right - left + 1, result);
        }
        result = Math.max(result, longestMountain(right, arr));
        return result < 3 ? 0 : result;
    }


    public static void main(String[] args) {
//        int[] a = {2, 1, 4, 7, 3, 2, 1};
//        int[]  a = {0,0,1,2,3,4,5,4,3,2,1,0};
        int[] a = {731, 723, 685};

        LongestMountainArray845 lm = new LongestMountainArray845();
        int i = lm.longestMountain(a);
        System.out.println(i);
    }
}
